package _interview150;

/**
 * 134. 加油站
 */
public class No134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        for (int i = 0; i < gas.length; i++) {
            gas[i] = gas[i] - cost[i];
        }
        int sum = 0, curSum = 0, ans = -1;
        for (int i = 0; i < gas.length; i++) {
            curSum += gas[i];
            sum += gas[i];
            if (curSum < 0) {
                curSum = 0;
                ans = -1;
            } else if (ans == -1) ans = i;
        }
        return sum >= 0 ? ans : -1;
    }
}
